package com.huanxing.cloud.auth.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.huanxing.cloud.common.core.constant.CommonConstants;
import com.huanxing.cloud.common.core.constant.SecurityConstants;
import com.huanxing.cloud.common.core.util.IpUtils;
import com.huanxing.cloud.security.entity.HuanxingUser;
import com.huanxing.cloud.security.utils.SecurityUtils;
import com.huanxing.cloud.upms.common.entity.SysLoginLog;
import com.huanxing.cloud.upms.common.feign.FeignSysLoginLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * @description: 用户登录成功监听器事件
 * @author lijx
 * @date 2022/5/20 22:30
 * @version 1.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class HxAuthencationSuccessListener
    implements ApplicationListener<AuthenticationSuccessEvent> {
  private final FeignSysLoginLogService feignSysLoginLogService;

  @Override
  public void onApplicationEvent(AuthenticationSuccessEvent event) {
    Authentication authentication = (Authentication) event.getSource();
    if (CollUtil.isNotEmpty(authentication.getAuthorities())) {
      HuanxingUser huanxingUser = SecurityUtils.getUser(authentication);
      HttpServletRequest request =
          ((ServletRequestAttributes)
                  Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
              .getRequest();
      // 异步处理登录日志
      CompletableFuture.runAsync(
          () -> {
            String agent = request.getHeader("User-Agent").toLowerCase();
            UserAgent ua = UserAgentUtil.parse(agent);
            SysLoginLog sysLoginLog = new SysLoginLog();
            sysLoginLog.setStatus(CommonConstants.LOGIN_LOG_STATUS_1);
            sysLoginLog.setIpAddr(ServletUtil.getClientIP(request));
            sysLoginLog.setLocation(IpUtils.getWhoisAddress(sysLoginLog.getIpAddr()));
            sysLoginLog.setUserName(huanxingUser.getUsername());
            sysLoginLog.setCreateId(huanxingUser.getUserId());
            sysLoginLog.setOs(ua.getOs().toString());
            sysLoginLog.setBrowser(ua.getBrowser().toString());
            sysLoginLog.setMsg(StrUtil.format("用户：{} 登录成功", huanxingUser.getUsername()));
            feignSysLoginLogService.save(sysLoginLog, SecurityConstants.SOURCE_IN);
          });
    }
  }
}
